<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>图书信息管理子系统</title>
    <link th:href="@{/js/layui/css/layui.css}" rel="stylesheet">
    <script th:src="@{/js/jquery.min.js}"></script>
    <script th:src="@{/js/jquery-form.js}"></script>
    <script th:src="@{/js/layui/layui.js}"></script>
    <style>
        .demo-reg-container {
            width: 320px;
            margin: 21px auto 0;
        }
    </style>
</head>
<body>
<form class="layui-form" method="post" th:action="@{/user/register}">
    <div class="demo-reg-container">
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="text" name="username" value="" lay-verify="required" placeholder="用户名" autocomplete="off"
                       class="layui-input" id="reg-username">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="password" value="" lay-verify="required" placeholder="密码" autocomplete="off"
                       class="layui-input" id="reg-password" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="comfirmPwd" value="" lay-verify="required|confirmPassword"
                       placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="register">注册</button>
        </div>
    </div>
</form>
<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;
        // 提交事件
        form.on('submit(register)', function (data) {
            $(".layui-form").ajaxForm({
                error: function(data){
                    errorHandle(data);
                },
                success: function(data) {
                    console.log(data)
                    if (data.code == 200) {
                        layer.msg('注册成功', {
                            icon: 1,
                            time: 500,
                            shade: [0.5, '#000'],
                            end: function() {
                                location.href = "[[@{/}]]login";
                            }
                        });
                    } else {
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 500,
                            shade: [0.5, '#000']
                        });
                    }
                }
            });
        });
    });

    // 错误处理
    function errorHandle(data) {
        if (data.status == 404) {
            layer.msg("请求资源不存在", {icon: 2});
        } else {
            layer.msg("服务端异常", {icon: 2});
        }
        return;
    }
</script>
</body>
</html>